package com.lly.songhigh.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.lly.songhigh.R;
import com.lly.songhigh.entity.SongSheetList;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by dllo on 15/10/24.
 */
public class SongSheetListAdapter extends BaseAdapter {

    private List<SongSheetList.DataEntity.ChildrenEntity> childrenEntities;
    private Context context;

    public SongSheetListAdapter(Context context, List<SongSheetList.DataEntity.ChildrenEntity> childrenEntities) {
        this.context = context;
        this.childrenEntities = childrenEntities;
    }

    @Override
    public int getCount() {
        return childrenEntities != null && childrenEntities.size() > 0 ? childrenEntities.size() : 0;
    }

    @Override
    public SongSheetList.DataEntity.ChildrenEntity getItem(int position) {
        return childrenEntities.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.song_sheet_list_item, null);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        SongSheetList.DataEntity.ChildrenEntity childrenEntity = getItem(position);
        viewHolder.songSheetListItemTv.setText(childrenEntity.getTag());
        return convertView;
    }

    /**
     * This class contains all butterknife-injected Views & Layouts from layout file 'song_sheet_list_item.xml'
     * for easy to all layout elements.
     *
     * @author ButterKnifeZelezny, plugin for Android Studio by Avast Developers (http://github.com/avast)
     */
    static class ViewHolder {
        @Bind(R.id.song_sheet_list_item_tv)
        TextView songSheetListItemTv;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
